import api from '../../http/apis/apis'

export default {
    namespaced: true,
    state: {
        SaleData: {
            pageSize: 5,
            currentPage: 1,
            totalPages: 2,
            total: 0,
            data: [{ _id: 0, username: 'lisi', tell: 13565914409, }],
        },
        ClientData: []
    },
    mutations: {
        //数据库信息给到state
        SetSaleDate(state, newSaleData) {
            state.SaleData = newSaleData;
            // console.log(state.SaleData);
        },
        //改变当前页
        setCurrent(state, newcurrent) {
            state.SaleData.currentPage = newcurrent;
        },
        //每页显示数量
        setPageSize(state, newpagesize) {
            state.SaleData.pageSize = newpagesize;
        },
        setClientData(state, newClientData) {
            state.ClientData = newClientData
        }

    },
    actions: {
        //新增销售
        async addSalesAsync(context, sale) {
            const { data } = await api.sales.addSales(sale);
            // console.log(data);
            if (data.state) {
                return data;
            } else {
                return data;
            }
        },
        //获取所有的销售
        async getSalesAsync(context) {
            const { data } = await api.sales.getAllSales({
                pageSize: context.state.SaleData.pageSize,
                currentPage: context.state.SaleData.currentPage,
            });
            // console.log(data);
            if (data.status) {
                context.commit('SetSaleDate', data.data);
            }
        },
        //删除销售
        async deleteOneAsync(context, _id) {
            const { data } = await api.sales.deleteOne(_id);
            // console.log(data);
            if (data.status) {
                context.dispatch('getSalesAsync');
            } else {
                alert('删除失败');
            }
        },
        //获取修改销售的信息
        async getUpdataOneAsync(context, _id) {
            const { data } = await api.sales.getUpdataSale(_id);
            // console.log(data);
            if (data.status) {
                return data;
            } else {
                return data;
            }
        },
        async SureupdataOneAsync(context, sale) {
            const { data } = await api.sales.sureUpdataOne(sale);
            // console.log(data);
            if (data.status) {
                return data;
            } else {
                return data;
            }

        },
        // 获取所有客户
        async getClients(context, _id) {
            const { data } = await api.sales.getClients()
            if (data.status) {
                const ClientsData = data.data.data
                const newData = ClientsData.filter((item) => {
                    return item.salesId._id == _id
                })
                context.commit("setClientData", newData)
                console.log(newData);
            }
        },
        async isLogin(context) {
            const { data } = await api.admin.isLogin()
            return data.data
        }


    },


}